<!--
  Copyright 2018 Google Inc. All rights reserved.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 -->

<link rel="shortcut icon" href="favicon.ico" sizes="256x256" />
<title>PhotoBooth App</title>
<style>
body {
  background-color: #e6e8ec;
  display: flex;
  flex-direction: column;
  margin: 0;
}

canvas {
  display: none;
}

.buttons {
  display: flex;
  justify-content: center;
  color: #aaa;
  flex-basis: 48px;
}

#button {
  cursor: hand;
  background-color: black;
  -webkit-mask-image: url(camera.svg);
  transform: scale(2, 2);
  width: 24px;
  height: 24px;
  margin-top: 12px;
}

#button:hover {
  background: red;
}

#button:active {
  opacity: 0.4;
}

#video {
  flex: auto;
  transform: scaleX(-1);
}

.flashit {
  animation: flash linear 100ms;
}

@keyframes flash {
  0% { opacity: 1; } 
  50% { opacity: .1; } 
  100% { opacity: 1; }
}

</style>

<script>
async function run(){  
  const video = document.getElementById('video');
  const button = document.getElementById('button');
  video.srcObject = await navigator.mediaDevices.getUserMedia({ video: true });
  button.addEventListener('click', () => captureScreenshot(), false);
}

function captureScreenshot() {
  const video = document.getElementById('video');
  const canvas = document.getElementById('canvas');
  canvas.width = video.videoWidth;
  canvas.height = video.videoHeight;
  const context = canvas.getContext('2d');
  context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
  saveImage(canvas.toDataURL('image/jpeg').substr('data:image/jpeg;base64,'.length));
  video.classList.add('flashit');
  setTimeout(() => video.classList.remove('flashit'), 1000);
}
</script>

<body onload="run()" tabIndex="0">
  <video autoplay id="video"></video>
  <canvas id="canvas"></canvas>
  <div class="buttons">
    <div id="button"/>
  </div>
</body>

</html>
